<?php
/*
 * 总栏目
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Menu extends Comm
{
    public function index()
    {
        $m = db('auth_rule');
        $list = $m->order('o asc')->select();
        $list = $this->getMenu($list);
        $this->assign('list', $list);
        return $this->fetch();
    }
	
    public function add()
    {

        $option = db('auth_rule')->order('o ASC')->select();
        $option = $this->getMenu($option);
        $this->assign('option', $option);
		return $this->fetch('form');
    }
	
    public function edit($id = 0)
    {
        $id = intval($id);
       // $m = DB::name('auth_rule');
        $currentmenu = DB::name('auth_rule')->where("id='$id'")->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }

        $option = DB::name('auth_rule')->order('o ASC')->select();
        $option = $this->getMenu($option);
        $this->assign('option', $option);
        $this->assign('currentmenu', $currentmenu);
        return $this->fetch('form');
    }
	
    public function update()
    {
        $id = input('post.id', '', 'intval');
        $data['pid'] = input('post.pid', '', 'intval');
        $data['title'] = input('post.title', '', 'strip_tags');
        $data['name'] = input('post.name', '', 'strip_tags');
        $data['icon'] = input('post.icon');
        $data['islink'] = input('post.islink', '', 'intval');
		$data['cs'] = input('post.cs');
        $data['status'] = 1;
        $data['o'] = input('post.o', '', 'intval');
        $data['tips'] = input('post.tips');
        if ($id) {
            Db::name('auth_rule')->where("id='{$id}'")->update($data);
            addlog('编辑菜单，ID：' . $id);
        } else {
            Db::name('auth_rule')->insert($data);
            addlog('新增菜单，名称：' . $data['title']);
        }
        //$this->success('操作成功！');
		return json(array('code' => 200, 'msg' => '操作成功'));	
    }

    public function del()
    {
        $ids = input('param.');
        if ($ids) {
            if (is_array($ids)) {
				if (request()->isGet()){
					$ids = implode(',', $ids);
				}else{
					$ids = implode(',', $ids['ids']);	
				}
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }
            if (db('auth_rule')->where($map)->delete()) {
                addlog('删除总栏目，ID：' . $ids);
                return json(array('code' => 200, 'msg' => '删除成功'));	
            } else {
                return json(array('code' => 0, 'msg' => '参数错误'));
            }
        } else {
            return json(array('code' => 0, 'msg' => '参数错误'));
        }
    }
	
    public function islink($id=null)
    {
        if (!$id) {
            $this->error('参数错误！');
        }
        if ($id == 1) {
            $this->error('控制台不可变更状态！');
        }
        $menu = db('auth_rule')->where('id=' . $id)->find();
        if (!$menu) {
            $this->error('参数错误！');
        }
        $islink = $menu['islink'];
        if ($islink == 1) {
           $res = db('auth_rule')->data(array('islink' => 0))->where('id=' . $id)->update();
        }
        if ($islink != 1 ) {
            $res = db('auth_rule')->data(array('islink' => 1))->where('id=' . $id)->update();
        }
        if ($res) {
			return json(array('code' => 200, 'msg' => '恭喜，更新状态成功！'));
        } else {
            $this->error('更新失败！');
        }
    }
	
	
	
}
